﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProjectCallbackTemplate<T> : GetTogether.Studio.Web.UI.PageCallback
    where T : class, new()
{
    public string _ProjectName;
    public string ProjectName
    {
        get
        {
            return string.IsNullOrEmpty(_ProjectName) ? Request["pn"] : _ProjectName;
        }
        set
        {
            _ProjectName = value;
        }
    }

    public string Content
    {
        get
        {

            string xml = Request["content"];
            if (!string.IsNullOrEmpty(xml)) xml = xml.Replace("&", "&amp;");
            return xml;
        }
    }

    public bool IsGetNewProject
    {
        get { return !string.IsNullOrEmpty(Request["get"]); }
    }

    public string ProjectUserControl;

    public string ProjectBasePath;

    public T EditProject;

    private string ProjectPath
    {
        get
        {
            return System.IO.Path.Combine(ProjectBasePath, ProjectName);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        switch (type)
        {
            case 0:
                GetProjects();
                break;
            case 1:
                CreateProjects();
                break;
            case 2:
                EditProjects();
                break;
            case 3:
                DeleteProject();
                break;
            default:
                break;
        }
    }

    #region Functions

    public virtual void GetProjects()
    {
        Control c = Page.LoadControl(ProjectUserControl);
        Page.Controls.Add(c);
    }

    public virtual string GetProjectContent(string xml, string btnText, string jsFunc)
    {
        return string.Concat(
                        "<div style='padding:3px 3px 3px 4px;'><div class='box' style='background-color:#fff;'><textarea id='txtProject' name='txtProject' style='width:100%;height:400px;' cols='50' rows='27'>",
                       xml.Replace("&amp;", "&"),
                        "</textarea></div><div id='dv-error' style='color:red;text-align:center;'></div>",
                        "<div style='text-align:center;padding:5px 5px 2px 5px;'><input type='button' onclick=\"",
                        jsFunc,
                        "\" class='btn' value='",
                        btnText,
                        "' />&nbsp;<input type='button' onclick='CloseMsgBox();' class='btn' value='Cancel' /></div></div>"
                        );
    }

    public virtual void EditProjects()
    {
        if (IsGetNewProject)
        {
            try
            {
                string xml = GetTogether.Utility.SerializationHelper.SerializeToXml(EditProject);
                Response.Write(GetProjectContent(xml, "Save", "ProjectExec('txtProject',2);"));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            Response.End();
        }
        else
        {
            try
            {
                System.IO.File.WriteAllText(ProjectPath, GetTogether.Utility.SerializationHelper.SerializeToXml(EditProject));
                Response.Write("OK");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }

    public virtual void CreateProjects()
    {
        if (IsGetNewProject)
        {
            Response.Write(GetProjectContent(GetTogether.Utility.SerializationHelper.SerializeToXml(new T()).Replace("<Category>Unknown</Category>", "<Category></Category>").Replace("  <ActionOn>0001-01-01T00:00:00</ActionOn>\r\n", ""), "Create", "ProjectExec('txtProject',1);"));
            Response.End();
        }
        else
        {
            try
            {
                if (System.IO.File.Exists(ProjectPath))
                {
                    Response.Write(string.Concat("The project \"", ProjectName, "\" already existed."));
                }
                else
                {
                    System.IO.File.WriteAllText(ProjectPath, GetTogether.Utility.SerializationHelper.SerializeToXml(EditProject));

                    Response.Write("OK");
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }

    public virtual void DeleteProject()
    {
        try
        {
            string path = ProjectPath;
            if (System.IO.File.Exists(path))
            {
                string historyPath = string.Concat(path, "(History)");
                if (System.IO.Directory.Exists(historyPath)) System.IO.Directory.Delete(historyPath, true);
                System.IO.File.Delete(path);
            }
            JsonSuccess();
        }
        catch (Exception ex)
        {
            GetTogether.Studio.Log.Error(ex);
            JsonError(ex.ToString());
        }
    }
    #endregion
}